<template>
  <header class="adm-header">
    <el-popover ref="popover" v-if="user"
      :value="showUserPanel" placement="bottom-end" popper-class="panel-popover"
        width="400" trigger="click" >
        <div class="control-panel">
          <div class="control-panel-top">
            <div class="control-panel-detai">
              <div class="control-panel-cicle">
                <img :src="user.headimgurl" />
              </div>
              <div class="control-panel-desc">
                <h4 class="control-panel-title">{{ user.nickname }}</h4>
                <span class="control-panel-txt">{{ user.username }}</span>
              </div>
            </div>
          </div>
          <div class="control-panel-body">
            <div class="control-panel-items">
              <router-link :to="{path: '/customs'}" class="control-item">
                <i class="user-new-ico control-ico">
                  <icon name="user-o"></icon>
                </i>
                <span class="control-name">客户管理</span>
              </router-link>
              <router-link :to="{path: '/channels'}" class="control-item">
                <i class="user-new-ico control-ico">
                  <icon name="file-powerpoint-o"></icon>
                </i>
                <span class="control-name">渠道管理</span>
              </router-link>
            </div>
          </div>

          <div class="control-panel-footer">
            <div class="control-panel-footer">
              <span @click="handleLogout">退出登录</span>
            </div>
          </div>
        </div>
    </el-popover>
    <div class="container clearfix">
      <TopNavBar></TopNavBar>
      <div class="link-group-main">
        <ul class="link-group-list clearfix">
          <li v-if="user" v-popover:popover>
              <router-link to="/user/login" v-if="!user">
                <i class="user-new-ico">
                  <icon name="user-circle-o"></icon>
                </i>
                <span>登录</span>
              </router-link>
              <div class="inline-block" v-if="user">
                <span class="bolder-txt">控制中心</span>
              </div>
            </li>
            <!-- <li v-if="!user">
              <router-link to="/user/register">
                <i class="user-new-ico">
                <icon name="user-plus"></icon>
                </i>
                <span>注册</span>
              </router-link>
            </li>  -->
        </ul>
      </div>
    </div>
  </header>
</template>
<style>
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
.clearfix{*+height:1%;}
</style>

<style lang="scss">
  @import "../scss/var.scss";
  $height: 56px;

  .panel-popover {
    box-shadow: 0 2px 12px rgba(0,0,0,.06);
    padding: 0;
  }


  .adm-header {
    height: $height;
    line-height: $height;
    // border-bottom: 2px solid #f4f6fb;
    box-shadow: 0 2px 2px #f4f6fb;
    width: 100%;
    right: 0;
    z-index: 999;
    background: white;
    position: absolute;
    box-shadow: 0 2px 2px #f4f6fb;


    .inline-block {
      display: inline-block;
      span {
        vertical-align: middle;
        // color: ;
      }
    }
    .router-link-active{
        text-decoration: none;
    }

    .logo-link{
        float: left;
    }

    .container, .page-container {
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        box-sizing: border-box;
    }



    .link-group-main{
        float: right;
        margin-right: 40px;
    }

    .link-group-list{
        margin:0 5px 0 0;
        padding: 0;

        li{
            float: left;
            list-style: none;
            font-size: 14px;
            color: white;
            margin-left: 26px;
        }

        li:first-child{
            margin-left: 0;
        }

        span{
            color: #48576a;
            display: inline-block;
            line-height: normal;
            vertical-align:middle;
            font-size: 15px;
            cursor:pointer;
            overflow:hidden;
            margin-top: 3px;
            text-decoration:none;

            i{
                display: block;
                margin-bottom: -2px;
            }
        }

    }
  }

  .avatar {
    height: 40px;
    overflow: hidden;
    display: inline-block;
    border-radius: 50%;
    width: 40px;

    img {
      width: 100%;
    }
  }

  .control-panel-detai {
    padding: 6px 22px 20px;
    overflow: hidden;
  }

  $control-panel-cicle-size: 58px;
  $control-panel-desc-size: 34px;
  .control-panel-cicle {
    float: left;
    color: #5e6166;
    overflow: hidden;
    display: inline-block;
    border-radius: 50%;
    width: $control-panel-cicle-size;
    height: $control-panel-cicle-size;
    background-color: #eef1f6;

    img {
      width: 100%;
    }
  }

  .control-panel-desc {
    float: left;
    margin-top: ($control-panel-cicle-size - $control-panel-desc-size)/2;
    margin-left: 10px;
  }

  .control-panel-title {
    margin: 2px 0;
    font-weight: bold;
    font-size: 14px;
  }

  .control-panel-top {
    border-bottom: 1px solid rgb(209, 219, 229);
  }

  .control-panel-body {
    padding: 20px 20px 0;
    border-bottom: 1px solid rgb(209, 219, 229);
  }

  .control-item {
    width: 25%;
    margin-bottom: 26px;
    text-align: center;
    float: left;
    cursor: pointer;
    color: inherit;
  }

  .control-panel-items {
    overflow: hidden;
  }

  .control-item .control-ico{
    display: block;
  }

  .control-item .control-name{
    display: block;
    margin: 8px 0 0;
    white-space: nowrap;
    width: 100%;
    font-size: 14px;
    font-weight: bolder;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .control-panel-footer {

    &>*{
      font-size: 14px;
      display: block;
      text-align: center;
      padding: 6px;
    }
  }
</style>

<script>
import TopNavBar from 'components/TopNavBar'
import { Popover } from 'element-ui'
import Icon from 'vue-awesome/components/Icon.vue'
import 'vue-awesome/icons/user-circle-o'
import 'vue-awesome/icons/user-plus'
import 'vue-awesome/icons/user-o'
import 'vue-awesome/icons/file-powerpoint-o'
import Config from '../config/common'

import { mapState, mapActions } from 'vuex'

export default {
  name: 'el-header',
  components: {
    'TopNavBar': TopNavBar,
    'icon': Icon,
    [Popover.name]: Popover
  },
  data () {
    return {
      logo: Config.IMG_HOST + '/crm.svg',
      showUserPanel: false
    }
  },
  computed: {
    ...mapState({
      user: state => state.auth.user
    })
  },
  methods: {
    handleLogout () {
      this.logout().then(() => {
        this.$router.push({ path: '/user/login' })
      })
    },
    ...mapActions({
      logout: 'logout'
    })
  },
  mounted () {
    const store = require('store')
    console.log(store.get('user'))
    console.log(JSON.stringify(this.user), '*******')
  }
}
</script>
